//
//  AppDelegateConfig.m
//  MUBATE
//
//  Created by sunbin on 2017/2/18.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "AppDelegateConfig.h"
#import <WeiboSDK.h>
#import "NewMessageModel.h"

@implementation AppDelegateConfig
{
    
    BQLDBTool *tool;//数据库（新消息插入本地数据库）

    NSDictionary *newMessageDic;
}

single_implementation(AppDelegateConfig);

- (BQLDBTool *)notiApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions appDelegate:(AppDelegate *)app{


    // 激光推送
    [self jpushDidFinishLaunchingWithOptions:launchOptions appDelegate:app];
    //设置全局
    [self globalConfiguration];
    // 分享操作 微信
    [self shareOperation];
    return [self createTheDatabase];

}
//1激光推送 注册方式
- (void)jpushDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions appDelegate:(AppDelegate *)app{

    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    // 3.0.0及以后版本注册可以这样写，也可以继续用旧的注册方式
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        //    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        //      NSSet<UNNotificationCategory *> *categories;
        //      entity.categories = categories;
        //    }
        //    else {
        //      NSSet<UIUserNotificationCategory *> *categories;
        //      entity.categories = categories;
        //    }
    }
    
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:app];
    //如不需要使用IDFA，advertisingIdentifier 可为nil
    [JPUSHService setupWithOption:launchOptions appKey:appKey
                          channel:channel
                 apsForProduction:isProduction
            advertisingIdentifier:advertisingId];
    
    //2.1.9版本新增获取registration id block接口。
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0){
            NSLog(@"registrationID获取成功：%@",registrationID);
        }
        else{
            NSLog(@"registrationID获取失败，code：%d",resCode);
        }
    }];


}



// 微信分享
- (void)shareOperation{
    //微信
    [WXApi registerApp:WXAppId];
    //向微信注册支持的文件类型
    UInt64 typeFlag = MMAPP_SUPPORT_TEXT | MMAPP_SUPPORT_PICTURE | MMAPP_SUPPORT_LOCATION | MMAPP_SUPPORT_VIDEO |MMAPP_SUPPORT_AUDIO | MMAPP_SUPPORT_WEBPAGE | MMAPP_SUPPORT_DOC | MMAPP_SUPPORT_DOCX | MMAPP_SUPPORT_PPT | MMAPP_SUPPORT_PPTX | MMAPP_SUPPORT_XLS | MMAPP_SUPPORT_XLSX | MMAPP_SUPPORT_PDF;
    
    [WXApi registerAppSupportContentFlag:typeFlag];
    
}

// 键盘风格
-(void)configureBoardManager{
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    manager.shouldResignOnTouchOutside = YES;
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    manager.keyboardDistanceFromTextField=44;
    manager.enableAutoToolbar = NO;
    
}

// 全局设置
- (void)globalConfiguration{
    //设置全局键盘样式
    [self configureBoardManager];
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor colorWithWhite:0.0f alpha:1.0f];
    shadow.shadowOffset = CGSizeMake(0, 0);
    [[UINavigationBar appearance] setTitleTextAttributes:@{
                                                           NSForegroundColorAttributeName: [UIColor colorWithHexString:@"fafafa"],
                                                           NSShadowAttributeName: shadow,
                                                           NSFontAttributeName: FONT_BarTitleAttributes
                                                           }];
    
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:[UIColor appNavigationBarColor]];
    
    
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];
}

// 创建数据库
- (BQLDBTool *)createTheDatabase{
    tool = [BQLDBTool instantiateTool];
    tool = [BQLDBTool instantiateTool];
    // 打开或者创建数据库(数据库要根据模型决定创建哪些字段)
    newMessageDic = @{@"tradingid"   :@"",
                      @"money"       :@"",
                      @"time"        :@"",
                      @"newmessagety"  :@"",
                      @"isRead"      :@"",
                      @"issuccessful":@""
                      };
    NewMessageModel *model = [NewMessageModel modelWithDictionary:newMessageDic];
    [tool openDBWith:NewMessageFile Model:model];
    return tool;
}

@end
